
function voteUp(entityName, entityId, userId){
	var button = event.target;
	$.ajax({
		type: "post",
		url: "/portal/ajax/voteUp/"+entityName,
		cache: false,				
		data: { entityId:entityId, userId:userId },
		success: function(data, text){
			console.log(data);
			if(data.status!=="error"){
				resolveVoteButtonsStyle(button);
				$(button).parent().parent().find("span.good-votes").html("+ "+data.goodVoteCount);
				$(button).parent().parent().find("span.bad-votes").html("- "+data.badVoteCount);
			}
		},
		error: function(){ }
	});
}

function voteDown(entityName, entityId, userId){
	var button = event.target;
	$.ajax({
		type: "post",
		url: "/portal/ajax/voteDown"+entityName,
		cache: false,				
		data: { entityId:entityId, userId:userId },
		success: function(data){
			if(data.status!=="error"){
				resolveVoteButtonsStyle(button);
				$(button).parent().parent().find("span.good-votes").html("+ "+data.goodVoteCount);
				$(button).parent().parent().find("span.bad-votes").html("- "+data.badVoteCount);
			}
		},
		error: function(){ }
	});
}

function resolveVoteButtonsStyle(selectedButton){
	console.log("resolveVoteButtonsStyle");
	if($(selectedButton).hasClass("plus")){
		$(selectedButton).removeClass("muted");
		$(selectedButton).removeClass("btn-default");
		$(selectedButton).addClass("btn-success");
		$(selectedButton).parent().find(".minus").addClass("muted");
	}else{
		$(selectedButton).removeClass("muted");
		$(selectedButton).removeClass("btn-default");
		$(selectedButton).addClass("btn-danger");
		$(selectedButton).parent().find(".plus").addClass("muted");
	}
}

function testAjaxJson(){
	$.ajax({
		type: "post",
		url: "/portal/testPage/testPost",
		cache: false,				
		success: function(response){
			console.log(response);
		},
		error: function(){	
			alert('Internal server error.');
		}
	});
}

